﻿using System;
using System.Windows.Forms;
using AccessControl.Business;
using AccessControl.Common;
using DevExpress.XtraEditors;


namespace AccessControl.Sida.FingerPrintCollector.Form
{
	public partial class MainForm : XtraForm
	{
		readonly FingerCollectionBusiness _fingerCollectionBusiness = new FingerCollectionBusiness();

		public MainForm()
		{
			InitializeComponent();
		}

		private void simpleButtonSearch_Click(object sender, EventArgs e)
		{
		    new IdentifyPerson().ShowDialog();
		}

	    private void SearchStudent()
	    {
	        if ((textEditStudentCode.Text == String.Empty) && (textEditName.Text == String.Empty) && (textEditFamily.Text == String.Empty))
	            return;
	        GetData();
	        textEditStudentCode.Text = String.Empty;
	        textEditName.Text = String.Empty;
	        textEditFamily.Text = String.Empty;
	        if (CurrentRow != null)
	        {
	            gridView.Focus();
	            simpleButtonAddFinger.Enabled = true;
	            if (gridView.RowCount==1)
	                simpleButtonAddFinger.PerformClick();
	        }
	        else
	        {
	            textEditStudentCode.Focus();
	            simpleButtonAddFinger.Enabled = false;
	        }
	    }

	    private void GetData()
		{
			var personBasicInfoDataTable = PersonBusiness.SelectByFilter(textEditStudentCode.Text, textEditName.Text, textEditFamily.Text);
            gridControl.DataSource = personBasicInfoDataTable ?? new AccessControlDS.tblPersonDataTable();
			
		}

		private void MainForm_KeyDown(object sender, KeyEventArgs e)
		{
			if (e.KeyCode == Keys.Enter)
				if (CurrentRow != null)
					simpleButtonAddFinger.PerformClick();
				else
					SearchStudent();
		}

		private void simpleButtonAddFinger_Click(object sender, EventArgs e)
		{
			
            var isRegistered = _fingerCollectionBusiness.IsStudentRegistered(CurrentRow.PersonRefNumber);
            if (isRegistered)
			{
				var result = XtraMessageBox.Show("اطلاعات اثر انگشت موجود می باشد، آیا مایل به ثبت مجدد می باشید؟",
                                                 "نرم افزار جمع آوری اثر انگشت  دانشجویان", MessageBoxButtons.YesNo,
				                                 MessageBoxIcon.Question);
				if (result==DialogResult.No)
					return;
				_fingerCollectionBusiness.DeleteByStudentCode(CurrentRow.PersonRefNumber);
			}
			new AddFingerPrintSimple(CurrentRow).ShowDialog();
		}

		private AccessControlDS.tblPersonRow CurrentRow
		{
			get
			{
				return gridView.SelectedRowsCount > 0
                        ? gridView.GetDataRow(gridView.GetSelectedRows()[0]) as AccessControlDS.tblPersonRow
						: null;
			}

		}

		private void gridView_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
		{
			if (e.Clicks != 2)
				return;
			simpleButtonAddFinger.PerformClick();
		}

		private void textEdit_Enter(object sender, EventArgs e)
		{
			gridControl.DataSource = null;
			simpleButtonAddFinger.Enabled = false;
		}

		private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
		{
			Application.Exit();
		}

	}
}